package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.dto.ChannelDto;
import com.heima.media.entity.WmChannel;
import com.heima.media.mapper.WmChannelMapper;
import com.heima.media.service.IWmChannelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements IWmChannelService {
    @Override
    public ResponseResult listByName(ChannelDto channelDto) {
        Page<WmChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(channelDto.getName())){
            queryWrapper.like(WmChannel::getName,channelDto.getName());
        }
        this.page(page,queryWrapper);
        return new PageResponseResult(channelDto.getPage(),channelDto.getSize(),page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult saveChannel(WmChannel wmChannel) {
        String name = wmChannel.getName();
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmChannel::getName,name);
        int count = this.count(queryWrapper);
        if(count==0){
            wmChannel.setIsDefault(false);
            wmChannel.setStatus(false);
            wmChannel.setOrd(0);
            this.save(wmChannel);
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"频道已存在!");
    }
}
